﻿using System;
using System.Collections.Generic;
using Dpx.Views;
using Xamarin.Forms;

namespace Dpx.Services {
    /// <summary>
    /// 内容页激活服务。
    /// </summary>
    public class ContentPageActivationService : IContentPageActivationService {
        private const string property = nameof(property);
        /// <summary>
        /// 页面键-页面类型字典。
        /// </summary>
        private readonly Dictionary<string, Type> _pageKeyTypeDictionary =
            new Dictionary<string, Type> {
                [ContentNavigationConstant.TodayDetailPage] =
                    typeof(TodayDetailPage),
                [ContentNavigationConstant.DetailPage] = typeof(DetailPage),
                [ContentNavigationConstant.ResultPage] = typeof(ResultPage),
            };

        /// <summary>
        /// 缓存。
        /// </summary>
        private readonly Dictionary<string, ContentPage> _cache =
            new Dictionary<string, ContentPage>();

        /// <summary>
        /// 激活。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        public ContentPage Activate(string pageKey) =>
            _cache.ContainsKey(pageKey)
                ? _cache[pageKey]
                : _cache[pageKey] =
                    Activator.CreateInstance(
                        _pageKeyTypeDictionary[pageKey]) as ContentPage;
    }
}